package com.example.controller;

import com.example.constant.RabbitmqConstant;
import com.example.pojo.Ticket;
import com.example.service.TicketFeignService;
import com.example.utils.RedisUtil;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/consumer")
public class TicketController {

    @Resource
    private TicketFeignService ticketFeignService;

    @Resource
    private RedisUtil redisUtil;
    @GetMapping("/selectAll")
    public List<Ticket> selectAll(){

        List<Ticket> list = ticketFeignService.selectAll();
        list.forEach(l->System.out.println("list="+l));

        return ticketFeignService.selectAll();
    }

    @PostMapping("/insertTicket")
    public void insertTicket(Ticket ticket){
        ticketFeignService.insertTicket(ticket);
    }

    @GetMapping("/getTicket/{id}")
    public Ticket getTicket(@PathVariable Long id){
        return ticketFeignService.getTicket(id);
    }

    @RabbitListener(queues = RabbitmqConstant.TICKET_UPDATE_QUEUE)
    public void updateTicket(long id){
        System.out.println("2222");
        System.out.println("consumer execute updateTicket,id="+id);
        if((int)redisUtil.get(String.valueOf(id))>0){
            redisUtil.decr(String.valueOf(id),1);
            System.out.println("库存为"+redisUtil.get(String.valueOf(id)));
        }else {
            System.out.println("库存不够，库存为"+redisUtil.get(String.valueOf(id)));
        }

    }

//    @DeleteMapping("/deleteTicket/{id}")
//    public boolean deleteTicket(@PathVariable Long id){
//        return ticketFeignService.deleteTicket(id);
//    }

    }

